<template>
  <div>
    <div>
      <el-row>
        <el-col>
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['news:message:template:add']"
            style="margin: 20px 20px 20px 20px"
          >
            新增模板
          </el-button>
        </el-col>
      </el-row>
    </div>
    <el-table
      v-loading="loading"
      :data="postList"
      :default-sort="{ prop: 'postId', order: 'descending' }"
    >
      <el-table-column label="模板名称" prop="" />
      <el-table-column label="模板内容" prop="" />
      <el-table-column label="添加时间" prop="" />
      <el-table-column label="场景说明" prop="" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['news:message:template:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['news:message:template:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="模板名称" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入模板名称" />
        </el-form-item>
        <el-form-item label="模板内容" prop="postCode">
          <el-input
            v-model="form.postCode"
            placeholder="请输入"
            type="textarea"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>
        <el-form-item>
          <div style="width: 400px;height: 80px;background-color: #f1f2f3;line-height: 25px;display:flex;flex-direction: column;">
            <span style="color:#757576;font-size: 11px;">变量限制:仅支持1个变量:4-6位汉数字、数字+字母组合或仅字母2种类型。</span>
            <span style="color:#757576;font-size: 11px;">变量格式:${code}:例如:您的验证码为${code}，该验证码5分钟内有效，请勿泄露于他人。</span>
          </div>
        </el-form-item>
        <el-form-item label="添加时间" prop="postCode">
          <el-date-picker
            v-model="value"
            type="datetime"
            placeholder="请选择"
          />
        </el-form-item>
        <el-form-item label="场景说明" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            maxlength="500"
            show-word-limit
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref } from "vue";
import { vacationRuleList } from "@/api/vacation/rule";
const { proxy } = getCurrentInstance();
const postList = ref([]);
const loading = ref(true);
const value = ref('')
const total = ref(0);
const open = ref(false);
const title = ref("");
const data = reactive({
  form: {},
  rules: {
    postName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
    postCode: [{ required: true, message: "名称不能为空", trigger: "blur" }],
    
  },
});

const { form, rules } = toRefs(data);
/** 查询短信模板列表 */
function getList() {
  loading.value = true;
  vacationRuleList({}).then((response) => {
    console.log(response, "请求数据");
    postList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增模板";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const postId = row.postId || ids.value;
  getPost(postId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改模板";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      if (form.value.postId != undefined) {
        updatePost(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPost(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 表单重置 */
function reset() {
  form.value = {
    postId: undefined,
    postCode: undefined,
    postName: undefined,
    postSort: 0,
    status: "0",
    remark: undefined,
  };
  proxy.resetForm("postRef");
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.postId || ids.value;
  proxy.$modal
    .confirm('是否确认删除广告位置配置为"' + postIds + '"的数据项？')
    .then(function () {
      return delPost(postIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
getList();
</script>
<style scoped>
:deep(.el-form-item) {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
</style>
